function love.load()
  createPhysics()
  
  love.graphics.setMode(1280,720,false,false,4)
  love.graphics.setBackgroundColor(0,0,0,255)
  
  turret = {}
  turret.i = love.graphics.newImage('turret.png')
  turret.x = 1280/2
  turret.y = 720/2
  turret.a = 0
  pingFade = love.graphics.newImage('pingFade.png')
end

function love.update(dt)  
  updateTurret(dt)
  world:update(dt)
  
  for i=1, #ping, 1 do
    ping[i].timer = ping[i].timer - 0.2
    if ping[i].timer < 0 then
      table.remove(ping,i)
      return
    end
  end
end
--
function updateTurret(dt)
  mx,my = love.mouse.getPosition()
  turret.a = -math.atan2(turret.x-mx, turret.y-my)
end
--
function love.mousepressed(x,y,b)
  if b == 'l' then
        local t = #ping + 1
        ping[t] = {}
        ping[t].body = love.physics.newBody(world, turret.x-turret.i:getWidth()/2,turret.y-9, 'dynamic')
        ping[t].shape = love.physics.newCircleShape(2)
        ping[t].fixture = love.physics.newFixture(ping[t].body,ping[t].shape)
        ping[t].fixture:setRestitution(1)
        local pa = turret.a - math.pi/2
        ping[t].body:applyLinearImpulse(math.cos(pa)*2, math.sin(pa)*2)
        ping[t].timer = 255
        ping[t].fade = pingFade
  end
end
function love.keypressed(k)
  if k == "a" then
    turret.x = turret.x - 20
  elseif k == "d" then
    turret.x = turret.x + 20
  elseif k == "w" then
    turret.y = turret.y - 20
  elseif k == "s" then
    turret.y = turret.y + 20
  end

end

--
function love.draw()
  stencil = function()
    love.graphics.circle('fill',turret.x,turret.y,128)
    for i = 1, #ping, 1 do
      if ping[i] ~= nil then
        love.graphics.circle('fill',ping[i].body:getX(),ping[i].body:getY(),64)
      end
    end
  end
  love.graphics.setStencil(stencil)
  
  --Borders
  love.graphics.setColor(0,255,255,255)
  love.graphics.polygon("fill", ground.body:getWorldPoints(ground.shape:getPoints()))
  love.graphics.polygon("fill", ceiling.body:getWorldPoints(ceiling.shape:getPoints()))
  love.graphics.polygon("fill", borderL.body:getWorldPoints(borderL.shape:getPoints()))
  love.graphics.polygon("fill", borderR.body:getWorldPoints(borderR.shape:getPoints()))
  
  --Rect
  for i=1, #rect, 1 do
  love.graphics.polygon('line', rect[i].body:getWorldPoints(rect[i].shape:getPoints()))
  end
  
  --Ball
  for i = 1, #ping, 1 do
    love.graphics.setColor(255,255,255,-ping[i].timer+255)
    love.graphics.draw(ping[i].fade, ping[i].body:getX()-ping[i].fade:getWidth()/2,ping[i].body:getY()-ping[i].fade:getHeight()/2)
    love.graphics.setColor(255,255,255,ping[i].timer)
    --love.graphics.circle('line',ping[i].body:getX(),ping[i].body:getY(),64)
    love.graphics.circle("fill", ping[i].body:getX(),ping[i].body:getY(), ping[i].shape:getRadius())
  end
  --Turret
  love.graphics.setColor(255,255,255,255)
  love.graphics.draw(turret.i, turret.x - turret.i:getWidth()/2, turret.y-turret.i:getHeight()/2, turret.a,1,1,8,14)
  
  love.graphics.print(love.timer.getFPS(),turret.x - 32,turret.y-32)
  end

--
--
function createPhysics()
  love.physics.setMeter(40)
  world = love.physics.newWorld(0, 0, true)
  
  ground = {}
  ground.body = love.physics.newBody(world, 640, 718)
  ground.shape = love.physics.newRectangleShape(1280, 4)
  ground.fixture = love.physics.newFixture(ground.body, ground.shape)
  ground.fixture:setRestitution(1)
  
  ceiling = {}
  ceiling.body = love.physics.newBody(world, 640, 2)
  ceiling.shape = love.physics.newRectangleShape(1280, 4)
  ceiling.fixture = love.physics.newFixture(ceiling.body, ceiling.shape)
  ceiling.fixture:setRestitution(1)
  
  borderL = {}
  borderL.body = love.physics.newBody(world, 2, 360)
  borderL.shape = love.physics.newRectangleShape(4, 720)
  borderL.fixture = love.physics.newFixture(borderL.body, borderL.shape)
  borderL.fixture:setRestitution(1)
  
  borderR = {}
  borderR.body = love.physics.newBody(world, 1278, 360)
  borderR.shape = love.physics.newRectangleShape(4, 720)
  borderR.fixture = love.physics.newFixture(borderR.body, borderR.shape)
  borderR.fixture:setRestitution(1)
  
  rect = {}
  rect[1] = {}
  rect[1].body = love.physics.newBody(world, 100, 300)
  rect[1].shape = love.physics.newRectangleShape(200, 100)
  rect[1].fixture = love.physics.newFixture(rect[1].body, rect[1].shape)
  rect[1].fixture:setRestitution(1)
  rect[2] = {}
  rect[2].body = love.physics.newBody(world, 1000, 300)
  rect[2].shape = love.physics.newRectangleShape(200, 100)
  rect[2].fixture = love.physics.newFixture(rect[2].body, rect[2].shape)
  rect[2].fixture:setRestitution(1)
  rect[3] = {}
  rect[3].body = love.physics.newBody(world, 1280/2, 600)
  rect[3].shape = love.physics.newRectangleShape(400, 100)
  rect[3].fixture = love.physics.newFixture(rect[3].body, rect[3].shape)
  rect[3].fixture:setRestitution(1)
  
  ping = {}
end